/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ddnsclient;

/**
 *
 * @author dvtrung
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author MINHHOANG
 */
public class MyConnection{

    private static Vector vHostname = null;

    public Vector getHostname() {
        return vHostname;
    }

    public boolean ConnectServer() {
        LoginGUI login = new LoginGUI();
        String strTemp;
//        Socket s = null;
//        BufferedReader br = null;
//        PrintWriter pw = null;
         Main.TimeUpdate = Integer.valueOf(login.getTextTimeUpdate());
        try {
            Main.s = new Socket(login.getDDNSServer(), 22000);
            Main.br = new BufferedReader(new InputStreamReader(Main.s.getInputStream()));
            Main.pw = new PrintWriter(Main.s.getOutputStream());
            System.out.println("Success");

            Main.pw.println(login.getUsername() + "-100-" + Main.revision++ + "-" + login.getPassword());
            Main.pw.flush();
            //revision ++;
            strTemp = Main.br.readLine();
            if (strTemp.startsWith(login.getUsername() + "-201")) {
                vHostname = new Vector();
                String tempHost = Main.br.readLine();
                while (!tempHost.startsWith(login.getUsername() + "-203")) {
                    //lấy tên hostname
                    String[] arr = tempHost.split("-", 4);
                    System.out.println("Hostname : " + arr[3]);
                    vHostname.addElement(arr[3]);
                    tempHost = Main.br.readLine();
                }
            }
        } catch (UnknownHostException ex)
        {
            return false;
        } catch (IOException ex)
        {
            return false;
        }
        if (!strTemp.startsWith(login.getUsername() + "-201")) {
            return false;
        }
        return true;
    }

}
